﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CycleTimeReport.CUiCTRSettings
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using Tecnomatix.Engineering.Olp.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.CycleTimeReport;

public class CUiCTRSettings : TxWindow, IComponentConnector, IStyleConnector
{
  internal CheckBox cbIgnoreUserTimers;
  internal CheckBox cbIgnorePriority;
  internal DataGrid dgTimerInfoList;
  internal DataGridTextColumn cTimerName;
  internal DataGridTemplateColumn cTimerPriority;
  internal DataGridCheckBoxColumn cTimerActive;
  internal Button btnOK;
  internal Button btnCancel;
  private bool _contentLoaded;

  private void LoadCheckedControlsStatus()
  {
    this.cbIgnorePriority.IsChecked = new bool?(CApCTRUtilities.GetBoolValueFromRegistry("Ignore timer priority", false));
    this.cbIgnoreUserTimers.IsChecked = new bool?(CApCTRUtilities.GetBoolValueFromRegistry("Ignore user timers", false));
  }

  private void StoreCheckedControlsStatus()
  {
    if (this.cbIgnorePriority.IsChecked.HasValue)
    {
      CApCTRUtilities.SetBoolValueToRegistry("Ignore timer priority", this.cbIgnorePriority.IsChecked.Value);
      TxOlpRoboticSimulationEventsManager.Instance.IgnoreTimerPriority = this.cbIgnorePriority.IsChecked.Value;
    }
    if (!this.cbIgnoreUserTimers.IsChecked.HasValue)
      return;
    CApCTRUtilities.SetBoolValueToRegistry("Ignore user timers", this.cbIgnoreUserTimers.IsChecked.Value);
    TxOlpRoboticSimulationEventsManager.Instance.IgnoreUserTimers = this.cbIgnoreUserTimers.IsChecked.Value;
  }

  public CUiCTRSettings() => this.InitializeComponent();

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this.dgTimerInfoList.ItemsSource = (IEnumerable) this.LoadCollectionData();
    this.LoadCheckedControlsStatus();
  }

  private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
  {
    if (e.Key != Key.Return)
      return;
    this.OK_apply();
  }

  private void btnCancel_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void btnOK_Click(object sender, RoutedEventArgs e) => this.OK_apply();

  private void OK_apply()
  {
    this.StoreCheckedControlsStatus();
    List<CApCTRSettingsTimerInfo> settingsTimerInfoList = new List<CApCTRSettingsTimerInfo>();
    this.StoreCollectionData(this.dgTimerInfoList.ItemsSource as List<CApCTRSettingsTimerInfo>);
    ((Window) this).Close();
  }

  private void StoreCollectionData(List<CApCTRSettingsTimerInfo> timerList)
  {
    TxOlpRoboticSimulationEventsManager.Instance.ClearTimerSettings();
    foreach (CApCTRSettingsTimerInfo timer in timerList)
    {
      CApCTRUtilities.SetSettingsTimerInfoToRegistry(timer.TimerName, timer);
      CApCTRUtilities.AddSettingsTimerInfoToEventsManager(timer);
    }
  }

  private List<CApCTRSettingsTimerInfo> LoadCollectionData()
  {
    List<CApCTRSettingsTimerInfo> settingsTimerInfoList = new List<CApCTRSettingsTimerInfo>();
    return CApCTRUtilities.LoadInternalTimerSettings();
  }

  private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  {
    if (!(sender is DataGridCell element) || element.IsEditing || element.IsReadOnly)
      return;
    if (!element.IsFocused)
      element.Focus();
    DataGrid visualParent1 = CUiCTRSettings.FindVisualParent<DataGrid>((UIElement) element);
    if (visualParent1 == null)
      return;
    if (visualParent1.SelectionUnit != DataGridSelectionUnit.FullRow)
    {
      if (element.IsSelected)
        return;
      element.IsSelected = true;
    }
    else
    {
      DataGridRow visualParent2 = CUiCTRSettings.FindVisualParent<DataGridRow>((UIElement) element);
      if (visualParent2 == null || visualParent2.IsSelected)
        return;
      visualParent2.IsSelected = true;
    }
  }

  private static T FindVisualParent<T>(UIElement element) where T : UIElement
  {
    for (UIElement reference = element; reference != null; reference = VisualTreeHelper.GetParent((DependencyObject) reference) as UIElement)
    {
      if (reference is T visualParent)
        return visualParent;
    }
    return default (T);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/cycletimereport/cuictrsettings.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  internal Delegate _CreateDelegate(Type delegateType, string handler)
  {
    return Delegate.CreateDelegate(delegateType, (object) this, handler);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((UIElement) target).PreviewKeyDown += new KeyEventHandler(this.Window_PreviewKeyDown);
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        break;
      case 2:
        this.cbIgnoreUserTimers = (CheckBox) target;
        break;
      case 3:
        this.cbIgnorePriority = (CheckBox) target;
        break;
      case 4:
        this.dgTimerInfoList = (DataGrid) target;
        break;
      case 5:
        this.cTimerName = (DataGridTextColumn) target;
        break;
      case 6:
        this.cTimerPriority = (DataGridTemplateColumn) target;
        break;
      case 7:
        this.cTimerActive = (DataGridCheckBoxColumn) target;
        break;
      case 9:
        this.btnOK = (Button) target;
        this.btnOK.Click += new RoutedEventHandler(this.btnOK_Click);
        break;
      case 10:
        this.btnCancel = (Button) target;
        this.btnCancel.Click += new RoutedEventHandler(this.btnCancel_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IStyleConnector.Connect(int connectionId, object target)
  {
    if (connectionId != 8)
      return;
    ((Style) target).Setters.Add((SetterBase) new EventSetter()
    {
      Event = UIElement.PreviewMouseLeftButtonDownEvent,
      Handler = (Delegate) new MouseButtonEventHandler(this.DataGridCell_PreviewMouseLeftButtonDown)
    });
  }
}
